

use "$home\persondata_v2.dta", clear

drop if lbnr==0
drop if cvrnr==.
*drop if multiple firm in one year
merge m:1 lbnr cvrnr year using "$project\merger_lbnr", keep(3) keepus(log_income_f) nogen
drop if log_income_f==.

gen byte manager=(isco>=1200&isco<=1399)
replace manager=1 if pstill==31

*link over time
bysort lbnr year: egen nmanager=sum(manager)
gen year_manager=year if manager==1
bysort lbnr pnr: egen minyear_manager=min(year_manager)
bysort lbnr pnr: gen nyear_firm=_N
replace manager=1 if nmanager==0&year>minyear_manager

gsort lbnr year -lonind
by lbnr year: gen incomerank=_n
bysort lbnr year: gen nworker=_N
cap drop nmanager
bysort lbnr year: egen nmanager=sum(manager)
bysort lbnr year: egen wagebill=sum(lonind)
bysort lbnr year: egen wage_second=max(lonind*(incomerank==2))
gen ratio=lonind*(nworker-1)/(wagebill-lonind)
gen byte toprank=(incomerank==1|incomerank<0.05*nworker)
replace toprank=0 if isco!=.&isco>=2000&pstill!=32
replace toprank=0 if ratio<1.6
replace toprank=1 if lonind/wage_second>1.5&lonind/wage_second<.
replace toprank=0 if nmanager>0
replace manager=1 if toprank==1

drop nmanager
bysort lbnr year: egen nmanager=sum(manager)
bysort lbnr year isco: gen temp0=_N
bysort lbnr year pstill: gen temp1=_N
replace manager=1 if incomerank==1&temp0==1&nmanager==0&lonind/wage_second>1.3&lonind/wage_second<.&isco<5000
replace manager=1 if incomerank==1&temp1==1&nmanager==0&lonind/wage_second>1.3&lonind/wage_second<.

********

*drop if nyear_firm==1

merge m:1 lbnr year using "$project\merger_lbnr", keep(3) keepus(target_cumu) nogen
drop if target_cumu>=1

keep if manager==1

bysort pnr: egen nfirm_manager=nvals(lbnr)
*keep if nfirm_manager>1

bysort pnr: egen long maxlbnr=max(lbnr)
bysort pnr: egen long minlbnr=min(lbnr)
bysort pnr: egen minyear=min(year)
bysort pnr: egen maxyear=max(year)
bysort pnr: egen nyear_maxlbnr=sum((lbnr==maxlbnr))
bysort pnr: egen nyear_minlbnr=sum((lbnr==minlbnr))

bysort maxlbnr minlbnr minyear maxyear nyear_maxlbnr nyear_minlbnr: egen double pnr_unique=min(pnr)
keep if pnr==pnr_unique

cap drop nfirm_manager nmanager

*keep only one manager per firm
bysort lbnr year: gen nmanager=_N
bysort lbnr year: egen nceo=sum((isco==1210))
drop if nmanager>1&nceo>0&isco!=1210
bysort pnr: egen nfirm_manager=nvals(lbnr)
drop if nfirm_manager==1
drop nmanager nfirm_manager
bysort lbnr year: gen nmanager=_N
bysort lbnr year: egen ntop=sum((pstill==31))
drop if nmanager>1&ntop>0&pstill!=31
bysort pnr: egen nfirm_manager=nvals(lbnr)
drop if nfirm_manager==1
drop nmanager nfirm_manager
bysort lbnr year: gen nmanager=_N
bysort lbnr pnr manager: gen nyear=_N
bysort lbnr year: egen maxnyear=max(nyear)
drop if nyear==1&maxnyear>1
bysort pnr: egen nfirm_manager=nvals(lbnr)
drop if nfirm_manager==1
drop nmanager nfirm_manager nyear maxnyear
bysort lbnr year: gen nmanager=_N
bysort lbnr pnr manager: gen nyear=_N
bysort lbnr year: egen maxnyear=max(nyear)
drop if nyear==2&maxnyear>2
bysort pnr: egen nfirm_manager=nvals(lbnr)
drop if nfirm_manager==1
drop nmanager nfirm_manager nyear maxnyear
bysort lbnr year: gen nmanager=_N

bysort lbnr year: egen minincomerank=min(incomerank)
drop if minincomerank<=5&incomerank>5
bysort pnr: egen nfirm_manager=nvals(lbnr)
drop if nfirm_manager==1
drop nmanager nfirm_manager minincomerank
bysort lbnr year: gen nmanager=_N
bysort lbnr year: egen minincomerank=min(incomerank)
drop if minincomerank<=3&incomerank>3
bysort pnr: egen nfirm_manager=nvals(lbnr)
drop if nfirm_manager==1
drop nmanager nfirm_manager minincomerank
bysort lbnr year: gen nmanager=_N
bysort lbnr year: egen minincomerank=min(incomerank)
drop if minincomerank<=2&incomerank>2
bysort pnr: egen nfirm_manager=nvals(lbnr)
drop if nfirm_manager==1
drop nmanager nfirm_manager minincomerank
bysort lbnr year: gen nmanager=_N
bysort lbnr year: egen minincomerank=min(incomerank)
drop if incomerank!=minincomerank
bysort pnr: egen nfirm_manager=nvals(lbnr)
drop if nfirm_manager==1
drop nmanager nfirm_manager minincomerank

keep lbnr year pnr log_income_f

save "$project\manager\manager_all", replace


use "$project\manager\manager_all", clear

merge 1:1 lbnr year using "$project\merger_lbnr", keep(1 3) keepus(cvrnr nworker) nogen

gsort cvrnr year -nworker

by cvrnr year: gen count=_n
keep if count==1

keep cvrnr year pnr

save "$project\manager\manager_all_cvrnr2", replace
